/**
 * 公共文件库的单例对象
 */
var PublicLibrary = {
	id : "publiclibraryTable",// 表格id
	seItem : null, // 选中的条目
	table : null,
	layerIndex : -1,
	classificationId : -1
};

/**
 * 初始化表格的列
 */
PublicLibrary.initColumn = function() {
	var columns = [ {
		field : 'selectItem',
		radio : true
	}, {
		title : 'id',
		field : 'id',
		visible : false,
		align : 'center',
		valign : 'middle'
	}, {
		title : '标题',
		field : 'filename',
		align : 'center',
		valign : 'middle',
		formatter:function (value,row,index){
			return "<a href='javascript:;' onclick='onlineView("+row.id+")'>"+value+"</a>";
		}
	}, {
		title : '创建时间',
		field : 'createtime',
		align : 'center',
		valign : 'middle'
	}, {
		title : '作者',
		field : 'author',
		align : 'center',
		valign : 'middle'
	}, {
		title : '审批状态',
		field : 'state',
		align : 'center',
		valign : 'middle'
	}, {
		title : '签出者',
		field : 'checkoutper',
		align : 'center',
		valign : 'middle'
	} ];
	return columns;
};

/**
 * 在线浏览
 * @param fileId
 * @returns
 */
function onlineView(fileId){
	//提交信息
    var ajax = new $ax(Feng.ctxPath + "/publiclibrary/view/"+fileId, function (data) {
    }, function (data) {
    	Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.start();
}

/**
 * 检查是否选中
 */
PublicLibrary.check = function() {
	var selected = $('#' + this.id).bootstrapTable('getSelections');
	if (selected.length == 0) {
		Feng.info("请先选中表格中的某一记录！");
		return false;
	} else {
		PublicLibrary.seItem = selected[0];
		return true;
	}
};

/**
 * 检查是否选中左侧节点
 */
PublicLibrary.checkNode = function() {
	var selected = PublicLibrary.classificationId;
	if (selected == -1) {
		Feng.info("请先选中某个类目！");
		return false;
	} else if (selected == 0) {
		Feng.info("该位置不可上传文件！");
		return false;
	} else {
		return true;
	}
};

/**
 * 点击上传文件
 */
PublicLibrary.openUpload = function() {
	if (this.checkNode()) {
		var index = layer.open({
			type : 2,
			title : '上传文件',
			area : [ '800px', '560px' ], // 宽高
			fix : false, // 不固定
			maxmin : true,
			content : Feng.ctxPath + '/publiclibrary/publiclibrary_upload/'
					+ PublicLibrary.classificationId
		});
		this.layerIndex = index;
	}
};

PublicLibrary.resetSearch = function() {
	$("#name").val("");
	$("#beginTime").val("");
	$("#endTime").val("");

	PublicLibrary.search();
}

PublicLibrary.search = function() {
	var queryData = {};

	queryData['pid'] = PublicLibrary.classificationId;
	queryData['name'] = $("#name").val();
	queryData['beginTime'] = $("#beginTime").val();
	queryData['endTime'] = $("#endTime").val();

	PublicLibrary.table.refresh({
		query : queryData
	});
}

PublicLibrary.onClickClassification = function(e, treeId, treeNode) {
	PublicLibrary.classificationId = treeNode.id;
	PublicLibrary.search();
};

$(function() {
	var defaultColunms = PublicLibrary.initColumn();
	var table = new BSTable("publiclibraryTable", "/publiclibrary/list",
			defaultColunms);
	table.setPaginationType("server");
	PublicLibrary.table = table.init();
	var ztree = new $ZTree("classificationTree", "/classification/tree");
	ztree.bindOnClick(PublicLibrary.onClickClassification);
	ztree.init();
});
